package com.schedule.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.schedule.common.R;
import com.schedule.entity.User;
import com.schedule.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *用户登录，session记录用户id
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody User user, HttpServletRequest request){
        log.info(user.toString());

        return userService.login(user,request);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R<User> save(@RequestBody User user){
        log.info(user.toString());
        return userService.register(user);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("userId");
        R<String> success = R.success(null);
        success.setMsg("退出成功");
        return success;
    }

    @PostMapping("/update")
    public R<User> update(@RequestBody User user){
        return userService.update(user);
    }
}
